layui.use('layer', function () {
    // 0.1 判断是否有token
    let token = localStorage.getItem("token");
    if (token !== null) {
        // 0.2 再从sessionStorage中查看有没有userInfo信息
        let myInfo_username = sessionStorage.getItem("myInfo_username");
        // 如果没有，则去服务器拿
        if (myInfo_username === null) {
            $.get("/user/myInfo", {"token": token}, function (result) {
                if (result.code === 200) {
                    sessionStorage.setItem("myInfo_username", result.data.username);//将userInfo存入sessionStorage中
                    sessionStorage.setItem("myInfo_headPortraitUrl", result.data.headPortraitUrl);//将userInfo存入sessionStorage中
                    $("li[name='user_li'] img").attr("src", result.data.headPortraitUrl);
                    $("li[name='user_li'] span").html(result.data.username);
                    $("li[name='user_li'] dd[name='not_login_to_hidden']").prop("hidden", false);
                } else if (result.code === 401) {
                    localStorage.removeItem("token");
                    sessionStorage.clear();
                    layer.msg(result.msg, {icon: 5});
                } else
                    layer.msg(result.msg, {icon: 5});
            })
        }
        // 如果有，则直接用
        else {
            $("li[name='user_li'] img").attr("src", sessionStorage.getItem("myInfo_headPortraitUrl"));
            $("li[name='user_li'] span").html(myInfo_username);
            $("li[name='user_li'] dd[name='not_login_to_hidden']").prop("hidden", false);
        }

    }
})

/*退出函数*/
function logout() {
    localStorage.removeItem("token");//这么做的目的是保留背景图样式
    sessionStorage.clear();
    window.location.reload();//重新加载页面
}

/*底部的动态loli图*/
function load_loli() {
    /*
    <div class="zoomIn a-time1 a-delay1" style="position: fixed; bottom:1em;right:1em;z-index:1000">

        <a href="#">
            <img style="width:5em;height:auto;" src="/blog/img/loli.gif" alt="">
        </a>
    </div>
    */
    if (!is_load())
        return false;

    let div = document.createElement("div");
    div.setAttribute("class", "zoomIn a-time1 a-delay1");
    div.setAttribute("style", "position: fixed; bottom:1em;right:1em;z-index:1000");

    let a = document.createElement("a");
    a.href = "#";

    let img = document.createElement("img");
    img.setAttribute("style", "width:5em;height:auto;");
    img.src = "https://z3.ax1x.com/2021/08/31/hUava4.gif";

    a.appendChild(img);
    div.appendChild(a);
    // 放入body标签中
    document.getElementsByTagName("body")[0].appendChild(div);
}

/*canvas的动态线条*/
function load_canvas() {
    if (!is_load())
        return false;
    let $ = layui.$;
    $("body").append('<script type="text/javascript" color="51,219,209" opacity="1" zIndex="100" count="250" src="/lib/canvas/canvas-nest.js"></script>')
}

/*用于过滤不需要动态图和动态线条的页面*/
function is_load() {
    // 需要过滤的页面：blog_detail.html
    let url = document.URL;
    let splits = url.split("/");
    let url_last = splits[splits.length - 1];
    let regex1 = /^blog_detail\.html/i;
    return !regex1.test(url_last);
}

// 加载动态图
load_loli();
// // 加载动态线条canvas
// load_canvas();